﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;

using EHTWebsite.Code;
/// <summary>
/// Author: toanvt
/// CreatedDate: 12/12/2011
/// Description: Thêm sửa Khách Hàng, Đối Tác
/// </summary>
public partial class Administrator_AdminCustomerIU : System.Web.UI.Page
{
    public Int32 khachHangID = HangSo.DEFAULT_ID;
    public Int32 nhomKhachHangID = HangSo.DEFAULT_ID;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            khachHangID = Int32.Parse(Request.QueryString["KhachHangID"].ToString());
        }
        catch (Exception)
        {
            khachHangID = HangSo.DEFAULT_ID;
        }

        try
        {
            nhomKhachHangID = Int32.Parse(Request.QueryString["NhomKhachHangID"].ToString());
        }
        catch (Exception)
        {
            nhomKhachHangID = HangSo.DEFAULT_ID;
        }

        if (!Page.IsPostBack)
        {
            LoadDanhMucKhachHang();

            if (khachHangID == -1)
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                this.imgHinhAnh.ImageUrl = @"~\ImageDatas\Products\noImage.gif";
                this.lbAdminCustomerIU.Text = "Thêm mới Khách Hàng, Đối Tác";
                this.ddlNhomKhachHang.SelectedValue = nhomKhachHangID.ToString();
            }
            else
            {
                LoadCustomer();
            }
        }

        // ẩn các validation cho các control other
        if (HangSo.USING_MULTIL_LANGUAGE == false)
        {
            rvDiaChiOther.Enabled = false;
            rvTenKhachHangOther.Enabled = false;
        }
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Khách hàng theo Mã khách hàng
    /// </summary>
    private void LoadDanhMucKhachHang()
    {
        DataSet dsDanhMucKhachHangs = DanhMucKhachHangProvider.GetDanhMucKhachHangs();

        this.ddlNhomKhachHang.DataValueField = "DanhMucKhachHangID";
        this.ddlNhomKhachHang.DataTextField = "TenDanhMucKhachHangVN";
        this.ddlNhomKhachHang.DataSource = dsDanhMucKhachHangs;
        this.ddlNhomKhachHang.DataBind();

        this.ddlNhomKhachHang.Items.Insert(0, new ListItem("--chọn danh mục khách hàng--", "-1"));
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Khách hàng theo Mã khách hàng
    /// </summary>
    private void LoadCustomer()
    {
        KhachHang theCustomers = KhachHangProvider.GetKhachHangByKhachHangID(khachHangID);

        if (theCustomers == null)
        {
            this.imgHinhAnh.ImageUrl = @"~\ImageDatas\Customers\noImage.gif";
            return;
        }

        txtTenKhachHangVN.Text = theCustomers.TenKhachHangVN.ToString();
        txtTenKhachHangOther.Text = theCustomers.TenKhachHangOther.ToString();
        txtDiaChiVN.Text = theCustomers.DiaChiVN.ToString();
        txtDiaChiOther.Text = theCustomers.DiaChiOther.ToString();
        txtDienThoai.Text = theCustomers.DienThoai.ToString();
        txtDiDong.Text = theCustomers.DiDong.ToString();
        txtEmail.Text = theCustomers.Email.ToString();
        txtWebsite.Text = theCustomers.Website.ToString();
        fckGhiChuVN.Value = theCustomers.GhichuVN.ToString();
        fckGhiChuOther.Value = theCustomers.GhiChuOther.ToString();
        ddlNhomKhachHang.SelectedValue = theCustomers.NhomKhachHang.ToString();
        ckPublish.Checked = theCustomers.Publish;
        txtNumberOfOrder.Text = theCustomers.NumberOfOrder.ToString();

        try
        {
            this.txtNgayThanhLap.Text = theCustomers.NgaySinh.ToString("MM/dd/yyyy");
        }
        catch (Exception)
        {
        }

        this.imgHinhAnh.ImageUrl = @"~\ImageDatas\Customers\" + theCustomers.HinhAnhUrl.ToString();
        ViewState["HinhAnhUrl"] = theCustomers.HinhAnhUrl.ToString();
        ViewState["ID"] = theCustomers.KhachHangID;
        ViewState["action"] = HangSo.ACTION_UPDATE;
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Kiểm tra dữ liệu nhập
    /// </summary>
    /// <returns>Trạng thái hợp lệ</returns>
    private Boolean ValidateData()
    {
        String message = "";
        Boolean isSuccess = true;
        string sExtendsion = ".gif|.GIF|.jpg|.JPG|.png|.PNG|.bmp|.BMP";
        
        if (this.ddlNhomKhachHang.SelectedValue == "-1")
        {
            message += "Chọn nhóm khách hàng <br />";
            isSuccess = false;
        }

        try
        {
            if (this.fuHinhAnhUrl.HasFile && sExtendsion.IndexOf(Path.GetExtension(fuHinhAnhUrl.PostedFile.FileName)) < 0)
            {
                message += "File ảnh không đúng <br />";
                isSuccess = false;
            }
        }
        catch (Exception)
        {
            isSuccess = false;
        }

        if (isSuccess == false)
        {
            this.ltrMessage.Text = message;
        }

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            KhachHang theKhachHang = new KhachHang();
            theKhachHang.TenKhachHangVN = this.txtTenKhachHangVN.Text;
            theKhachHang.TenKhachHangOther = this.txtTenKhachHangOther.Text;
            theKhachHang.DiaChiVN = this.txtDiaChiVN.Text;
            theKhachHang.DiaChiOther = this.txtDiaChiOther.Text;
            theKhachHang.NhomKhachHang = Int32.Parse(ddlNhomKhachHang.SelectedValue.ToString());
            theKhachHang.Publish = ckPublish.Checked;

            try
            {
                theKhachHang.NgaySinh = DateTime.Parse(this.txtNgayThanhLap.Text.Trim());
            }
            catch (Exception)
            {
            }

            try
            {
                theKhachHang.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.ToString());
            }
            catch (Exception ex)
            {
                theKhachHang.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }
            theKhachHang.DienThoai = this.txtDienThoai.Text;
            theKhachHang.DiDong = this.txtDiDong.Text;
            theKhachHang.Email = this.txtEmail.Text;
            theKhachHang.Website = this.txtWebsite.Text;
            theKhachHang.GhichuVN = this.fckGhiChuVN.Value;
            theKhachHang.GhiChuOther = this.fckGhiChuOther.Value;
            theKhachHang.NhomKhachHang = Int32.Parse(ddlNhomKhachHang.SelectedValue.ToString());
            theKhachHang.Publish = this.ckPublish.Checked;
            theKhachHang.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.ToString());

            if (this.fuHinhAnhUrl.HasFile)
            {
                String fileName = this.fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                this.fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Customers//" + newFileName);
                theKhachHang.HinhAnhUrl = newFileName;
            }
            else
            {
                if (action == HangSo.ACTION_ADD)
                {
                    theKhachHang.HinhAnhUrl = "noImage.gif";
                }
                else
                {
                    theKhachHang.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theKhachHang.KhachHangID = -1;
                KhachHangProvider.IUKhachHang(theKhachHang, true);
            }
            else
            {
                theKhachHang.KhachHangID = Int32.Parse(ViewState["ID"].ToString());
                KhachHangProvider.IUKhachHang(theKhachHang, false);
            }

            Response.Redirect("AdminCustomers.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminCustomers.aspx");
    }

    /// <summary>
    /// Author:tungvu90
    /// CreatebyDate:06/02/2012
    /// Description:Thực hiện nút áp dụng khách hàng
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btlApply_Click(object sender, EventArgs e)
    {
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        Page.Validate();

        if (Page.IsValid)
        {
            KhachHang theKhachHang = new KhachHang();
            theKhachHang.TenKhachHangVN = this.txtTenKhachHangVN.Text;
            theKhachHang.TenKhachHangOther = this.txtTenKhachHangOther.Text;
            theKhachHang.DiaChiVN = this.txtDiaChiVN.Text;
            theKhachHang.DiaChiOther = this.txtDiaChiOther.Text;
            theKhachHang.NhomKhachHang = Int32.Parse(ddlNhomKhachHang.SelectedValue.ToString());
            theKhachHang.Publish = ckPublish.Checked;

            try
            {
                theKhachHang.NgaySinh = DateTime.Parse(this.txtNgayThanhLap.Text.Trim());
            }
            catch (Exception)
            {
            }

            try
            {
                theKhachHang.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.ToString());
            }
            catch (Exception ex)
            {
                theKhachHang.NumberOfOrder = 1;
                this.txtNumberOfOrder.Text = "1";
            }
            theKhachHang.DienThoai = this.txtDienThoai.Text;
            theKhachHang.DiDong = this.txtDiDong.Text;
            theKhachHang.Email = this.txtEmail.Text;
            theKhachHang.Website = this.txtWebsite.Text;
            theKhachHang.GhichuVN = this.fckGhiChuVN.Value;
            theKhachHang.GhiChuOther = this.fckGhiChuOther.Value;
            theKhachHang.NhomKhachHang = Int32.Parse(ddlNhomKhachHang.SelectedValue.ToString());
            theKhachHang.Publish = this.ckPublish.Checked;
            theKhachHang.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.ToString());

            if (this.fuHinhAnhUrl.HasFile)
            {
                String fileName = this.fuHinhAnhUrl.PostedFile.FileName;
                String fileType = Path.GetExtension(fileName);
                String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                this.fuHinhAnhUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Customers//" + newFileName);
                theKhachHang.HinhAnhUrl = newFileName;
            }
            else
            {
                if (action == HangSo.ACTION_ADD)
                {
                    theKhachHang.HinhAnhUrl = "noImage.gif";
                }
                else
                {
                    theKhachHang.HinhAnhUrl = ViewState["HinhAnhUrl"].ToString();
                }
            }

            if (action == HangSo.ACTION_ADD)
            {
                theKhachHang.KhachHangID = -1;
                KhachHangProvider.IUKhachHang(theKhachHang, true);
            }
            else
            {
                theKhachHang.KhachHangID = Int32.Parse(ViewState["ID"].ToString());
                KhachHangProvider.IUKhachHang(theKhachHang, false);
            }

            //Xuất thông báo lưu thành công
            this.lbMessage.CssClass = "BlueCorlor";
            this.lbMessage.Text = "Lưu thành công";
        }
    }
}